package com.dong.controller.admin;

import com.dong.config.TitleConfig;
import com.dong.exception.CodeMsg;
import com.dong.exception.Result;
import com.dong.service.GoodService;
import com.dong.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@RequestMapping("admin/good")
@Controller
public class GoodController {

    @Autowired
    private GoodService goodService;
    @Autowired
    private TitleConfig titleConfig;

    @GetMapping("list")
    public String list(Model model, @RequestParam(value = "name",required = false,defaultValue = "") String name, PageResult pageResult){
        model.addAttribute("title", titleConfig.getGoodTitle());
        model.addAttribute("name",name );
        model.addAttribute("pageResult",goodService.selectPages(pageResult,name ));
        return "/admin/good/list";
    }

    @DeleteMapping("delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(value = "id") Integer id){
        try {
          goodService.deleteGood(id);
        } catch (Exception e) {
            return Result.exception(CodeMsg.ADMIN_GOODS_DELETE_ERROR);
        }
        return Result.success(true);
    }
}
